package com.seqb.manage.guide.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.guide.domain.GuideDO;
import com.seqb.manage.guide.service.GuideService;
import com.seqb.manage.subject.service.SubjectService;
import com.seqb.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lilinshen
 * @title
 * @description 请填写相关描述
 * @date 2020-01-02
 */
@Controller
@RequestMapping("/manage/guide/guide")
public class GuideController extends BaseController {
    @Autowired
    private GuideService guideService;
    @Autowired
    private SubjectService subjectService;

    @GetMapping()
    @RequiresPermissions("manage:guide:guide:list")
    @Log("列表")
    String Guide(Model model) {
        model.addAttribute("subject", subjectService.list(null));
        return "manage/guide/guide.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manage:guide:guide:list")
    @Log("列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<GuideDO> page = guideService.list(query);
        return new PageUtils(page.getResult(), page.getTotal());
    }

    @GetMapping("/add")
    @RequiresPermissions("manage:guide:guide:add")
    @Log("添加")
    String add() {
        return "manage/guide/guide.add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("manage:guide:guide:edit")
    @Log("编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        GuideDO guide = guideService.get(id);
        model.addAttribute("guide", guide);
        model.addAttribute("subject", subjectService.list(new HashMap<>()));
        return "manage/guide/guide.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("manage:guide:guide:add")
    @Log("添加保存")
    public ResultVo save(GuideDO guide) {
        if (guideService.save(guide) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("编辑保存")
    @RequiresPermissions("manage:guide:guide:edit")
    public ResultVo update(GuideDO guide) {
        guideService.update(guide);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("manage:guide:guide:remove")
    @Log("删除")
    public ResultVo remove(Long id) {
        if (guideService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("manage:guide:guide:remove")
    @Log("批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        guideService.batchRemove(ids);
        return ResultVo.ok();
    }

}